package huawei.exam3;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/6/8
 */
public class HJ45 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            int sum = 0;
            int val = 26;
            String str = scanner.nextLine();
            Map<Character, Integer> countMap = orderByCountDesc(str);
            for (Map.Entry<Character,Integer> entry: countMap.entrySet()) {
                sum += entry.getValue() * val;
                val --;
            }
            System.out.println(sum);
        }
    }

    /**
     * 统计每个字符出现的次数，按照出现次数倒序排序，不区分大小写
     * @param str
     * @return
     */
    private static Map<Character,Integer> orderByCountDesc(String str) {
        Map<Character, Integer> hashMap = new HashMap<>(str.length());
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                c = Character.toLowerCase(c);
            }
            if (hashMap.containsKey(c)) {
                hashMap.put(c,hashMap.get(c) + 1);
            } else {
                hashMap.put(c,1);
            }
        }
        List<Map.Entry<Character, Integer>> entryList = hashMap.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()))
                .collect(Collectors.toList());
        Map<Character, Integer> linkedHashMap = new LinkedHashMap<>(entryList.size());
        entryList.forEach(entry -> {
            linkedHashMap.put(entry.getKey(),entry.getValue());
        });
        return linkedHashMap;
    }
}
